clear all 

use Data_MadagascarIE_Open.dta, clear 

// Table 2. Balance check of student characteristics in the baseline survey
* All grades 
global vlist student_age_b student_sex_b repeat_b lit_lv_b beginner_b letter_b word_b para_b story_b math_b math_std_b_check irt_score_base irt_score_std_b_check

iebaltab $vlist , grpvar(treat) grplabels(0 Control @ 1 Treatment) order(1 0) vce(cluster School_Code) cov(i.strataFE) rowvarlabels onerow save(temp.xlsx) replace

* student, G3 
iebaltab $vlist if Grade==3, grpvar(treat) grplabels(0 Control @ 1 Treatment) order(1 0) vce(cluster School_Code) cov(i.strataFE) rowvarlabels onerow save(temp.xlsx) replace

* student, G4
iebaltab $vlist if Grade==4, grpvar(treat) grplabels(0 Control @ 1 Treatment) order(1 0) vce(cluster School_Code) cov(i.strataFE) rowvarlabels onerow save(temp.xlsx) replace

* student, G5
iebaltab $vlist if Grade==5, grpvar(treat) grplabels(0 Control @ 1 Treatment) order(1 0) vce(cluster School_Code) cov(i.strataFE) rowvarlabels onerow save(temp.xlsx) replace

* Standard deviation of math test scores (points)
tabstat math_b, by(treat) s(sd)
tabstat math_b if Grade==3, by(treat) s(sd)
tabstat math_b if Grade==4, by(treat) s(sd)
tabstat math_b if Grade==5, by(treat) s(sd)

// Table 3. Comparison of school and SMC characteristics in the baseline survey
* School 
preserve 
duplicates drop School_Code, force 
iebaltab num_stu_tot ratio_female_stu Multi_grade_T3 Multi_grade_T4 Multi_grade_T5 ratio_drop_T3 ratio_drop_T4 ratio_drop_T5 ratio_rep_T3 ratio_rep_T4 ratio_rep_T5 ratio_pass_cepe ratio_pass_6th hm_exp hm_nocertificate hm_incharge num_teacher str ratio_fonction ratio_contract ratio_enfsub ratio_enfnonsub ratio_nocertificate num_classroom sch_fac_elec sch_fac_water sch_fac_toilet sch_fac_library sch_fac_kitchen, grpvar(treat) grplabels(0 Control @ 1 Treatment) order(1 0) vce(robust) cov(i.strataFE) rowvarlabels save(temp.xlsx) replace
restore

* SMC
preserve 
duplicates drop School_Code, force 
iebaltab yr_SMC president_exp pec share vol_resource tot_amount_vol caisseecole sae tot_amount_ex, grpvar(treat) grplabels(0 Control @ 1 Treatment) order(1 0) vce(robust) cov(i.strataFE) rowvarlabels save(temp.xlsx) replace
restore


* Drop attritors
drop if attrit==1

* Prepare variables for Athey and Imbens' specification 
run Vars_AtheyImbens.do


// Table 4. Impact on math scores
eststo clear
global xlist_reg student_age_b_dota-Treat_grade5_dota
eststo: reg irt_score_std treat *irt_score_std_b_dota i.strataFE $xlist_reg, cl(School_Code)

global xlist_reg student_age_b_dot3-Treat_sch_fac_kitchen_dot3
eststo: reg irt_score_std treat *irt_score_std_b_dot3 i.strataFE $xlist_reg if Grade==3, cl(School_Code)

global xlist_reg student_age_b_dot4-Treat_sch_fac_kitchen_dot4
eststo: reg irt_score_std treat *irt_score_std_b_dot4 i.strataFE $xlist_reg if Grade==4, cl(School_Code)

global xlist_reg student_age_b_dot5-Treat_sch_fac_kitchen_dot5
eststo: reg irt_score_std treat *irt_score_std_b_dot5 i.strataFE $xlist_reg if Grade==5, cl(School_Code)
esttab using temp.csv, star(* 0.1 ** 0.05 *** 0.01) drop(*.strataFE) nogaps depvars b(3) se(3) replace


// Table 5. Impact on basic reading level
* All Grades 
global xlist_reg letter_b_dota-Treat_story_b_dota
global xlist_reg2 student_age_b_dota-Treat_grade5_dota

eststo clear
eststo: reg beginner treat i.strataFE $xlist_reg $xlist_reg2, cl(School_Code)
eststo: reg letter treat i.strataFE $xlist_reg $xlist_reg2, cl(School_Code)
eststo: reg word treat i.strataFE $xlist_reg $xlist_reg2, cl(School_Code)
eststo: reg para treat i.strataFE $xlist_reg $xlist_reg2, cl(School_Code)
eststo: reg story treat i.strataFE $xlist_reg $xlist_reg2, cl(School_Code)
esttab using temp.csv, star(* 0.1 ** 0.05 *** 0.01) drop(*.strataFE) nogaps depvars b(3) se(3) replace

* Grade3
global xlist_reg letter_b_dot3-Treat_story_b_dot3
global xlist_reg2 student_age_b_dot3-Treat_sch_fac_kitchen_dot3

eststo clear
eststo: reg beginner treat i.strataFE $xlist_reg $xlist_reg2 if Grade==3, cl(School_Code)
eststo: reg letter treat i.strataFE $xlist_reg $xlist_reg2 if Grade==3, cl(School_Code)
eststo: reg word treat i.strataFE $xlist_reg $xlist_reg2 if Grade==3, cl(School_Code)
eststo: reg para treat i.strataFE $xlist_reg $xlist_reg2 if Grade==3, cl(School_Code)
eststo: reg story treat i.strataFE $xlist_reg $xlist_reg2 if Grade==3, cl(School_Code)
esttab using temp.csv, star(* 0.1 ** 0.05 *** 0.01) drop(*.strataFE) nogaps depvars b(3) se(3) replace

* Grade4
global xlist_reg letter_b_dot4-Treat_story_b_dot4
global xlist_reg2 student_age_b_dot4-Treat_sch_fac_kitchen_dot4

eststo clear
eststo: reg beginner treat i.strataFE $xlist_reg $xlist_reg2 if Grade==4, cl(School_Code)
eststo: reg letter treat i.strataFE $xlist_reg $xlist_reg2 if Grade==4, cl(School_Code)
eststo: reg word treat i.strataFE $xlist_reg $xlist_reg2 if Grade==4, cl(School_Code)
eststo: reg para treat i.strataFE $xlist_reg $xlist_reg2 if Grade==4, cl(School_Code)
eststo: reg story treat i.strataFE $xlist_reg $xlist_reg2 if Grade==4, cl(School_Code)
esttab using temp.csv, star(* 0.1 ** 0.05 *** 0.01) drop(*.strataFE) nogaps depvars b(3) se(3) replace

* Grade5
global xlist_reg letter_b_dot5-Treat_story_b_dot5
global xlist_reg2 student_age_b_dot5-Treat_sch_fac_kitchen_dot5

eststo clear
eststo: reg beginner treat i.strataFE $xlist_reg $xlist_reg2 if Grade==5, cl(School_Code)
eststo: reg letter treat i.strataFE $xlist_reg $xlist_reg2 if Grade==5, cl(School_Code)
eststo: reg word treat i.strataFE $xlist_reg $xlist_reg2 if Grade==5, cl(School_Code)
eststo: reg para treat i.strataFE $xlist_reg $xlist_reg2 if Grade==5, cl(School_Code)
eststo: reg story treat i.strataFE $xlist_reg $xlist_reg2 if Grade==5, cl(School_Code)
esttab using temp.csv, star(* 0.1 ** 0.05 *** 0.01) drop(*.strataFE) nogaps depvars b(3) se(3) replace

* Average in the control group (baseline)
tabstat beginner_b letter_b word_b para_b story_b if treat==0, by(Grade)


// Table 6. Impact on school‒level educational outcomes
duplicates drop School_Code, force 

* Prepare variables for Athey and Imbens' specification 
foreach var of varlist num_stu_tot hm_incharge ratio_fonction num_classroom sch_fac_water sch_fac_toilet sch_fac_library sch_fac_kitchen Multi_grade_T3 Multi_grade_T4 Multi_grade_T5{
	qui su `var'
	gen `var'_dots=`var'-r(mean)
	gen  Treat_`var'_dots=treat*`var'_dots
}

* Without covariates
eststo clear
foreach var of varlist ratio_drop_tot_f ratio_drop_T*_f ratio_rep_tot_f ratio_rep_T*_f ratio_pass_cepe_f ratio_pass_6th_f{
	eststo: reg `var' treat i.strataFE, robust 

}
esttab using temp.csv, star(* 0.1 ** 0.05 *** 0.01) drop(*.strataFE) nogaps depvars b(3) se(3) replace
 
* With covariates
global xlist_reg num_stu_tot_dots-Treat_sch_fac_kitchen_dots

eststo clear
eststo: reg ratio_drop_tot_f treat $xlist_reg Multi_grade_T3_dots Treat_Multi_grade_T3_dots Multi_grade_T4_dots Treat_Multi_grade_T4_dots Multi_grade_T5_dots Treat_Multi_grade_T5_dots i.strataFE, robust 

foreach i in 3 4 5 {
	eststo: reg ratio_drop_T`i'_f treat $xlist_reg Multi_grade_T`i'_dots Treat_Multi_grade_T`i'_dots i.strataFE, robust 
}

eststo: reg ratio_rep_tot_f treat $xlist_reg Multi_grade_T3_dots Treat_Multi_grade_T3_dots Multi_grade_T4_dots Treat_Multi_grade_T4_dots Multi_grade_T5_dots Treat_Multi_grade_T5_dots i.strataFE, robust 

foreach i in 3 4 5 {
	eststo: reg ratio_rep_T`i'_f treat $xlist_reg Multi_grade_T`i'_dots Treat_Multi_grade_T`i'_dots i.strataFE, robust 
}

foreach var of varlist ratio_pass_cepe_f ratio_pass_6th_f{
	eststo: reg `var' treat $xlist_reg  Multi_grade_T5_dots Treat_Multi_grade_T5_dots i.strataFE, robust 
}

esttab using temp.csv, star(* 0.1 ** 0.05 *** 0.01) drop(*.strataFE) nogaps depvars b(3) se(3) replace


